package manongfanshen.waitset;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import util.ThreadUtil;

/**
 * @author ChangLiang
 * @date 2020/3/18
 */
public class MainTestV1 {
    
    private final Logger logger = LoggerFactory.getLogger(getClass());

    private static final Object LOCK = new Object();

    /**
     * 线程被唤醒后，必须重新获取锁
     * notify之后，不需要再去Begin语句 而是拿到对象的锁标记 之后就进入到Runnable状态
     * 之后继续执行下面的指令
     */
    @Test
    @DisplayName("线程被唤醒后，必须重新获取锁")
    public void test() {
        new Thread(){
            @Override
            public void run() {
                work();
            }
        }.start();

        ThreadUtil.sleepForMilliSeconds(1000);

        synchronized (LOCK) {
            LOCK.notify();
        }

    }

    private void work() {
        synchronized (LOCK) {
            logger.info("Begin....");

            try {
                logger.info("Thread will coming");
                LOCK.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            logger.info("Thread will out.");
        }
    }
}
